import 'package:flutter/material.dart';
import 'package:refresh_page/refresh_page.dart';
import 'package:refresh_page/src/typedefs.dart';

class RefreshGrid<T, C extends RefreshPageController> extends StatelessWidget {
  final ItemWidgetBuilder<T> itemBuilder;
  final List<T> items;
  final int crossAxisCount;
  final double crossAxisSpacing;
  final double mainAxisSpacing;
  final double childAspectRatio;
  final ItemActionBuilder<T>? onTap;
  final EdgeInsetsGeometry? padding;
  final String? tag;
  final bool enablePullUp;
  final bool enablePullDown;
  final ScrollController? controller;
  final ItemActionBuilder<T>? onLongPress;

  /// 自定义加载中状态构建器，优先级高于全局配置
  final LoadingBuilder? loadingBuilder;

  /// 自定义空状态构建器，优先级高于全局配置
  final EmptyBuilder? emptyBuilder;

  /// 自定义错误状态构建器，优先级高于全局配置
  final ErrorBuilder? errorBuilder;

  /// 自定义状态显示位置构建器，允许用户自定义状态显示位置
  final Widget Function(Widget stateWidget)? statePositionBuilder;

  /// 空状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool emptyOverlay;

  /// 错误状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool errorOverlay;

  /// 加载状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool loadingOverlay;

  const RefreshGrid({
    super.key,
    required this.itemBuilder,
    required this.items,
    this.crossAxisCount = 3,
    this.crossAxisSpacing = 10,
    this.mainAxisSpacing = 10,
    this.childAspectRatio = 0.7,
    this.onTap,
    this.padding,
    this.tag,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.controller,
    this.onLongPress,
    this.loadingBuilder,
    this.emptyBuilder,
    this.errorBuilder,
    this.statePositionBuilder,
    this.emptyOverlay = true,
    this.errorOverlay = true,
    this.loadingOverlay = true,
  });

  @override
  Widget build(BuildContext context) {
    final pageController = Get.find<C>(tag: tag);
    return GetBuilder<C>(
      tag: tag,
      id: pageController.refreshId,
      builder: (_) {
        return RefreshWidget<C>(
          tag: tag,
          enablePullUp: enablePullUp,
          enablePullDown: enablePullDown,
          loadingBuilder: loadingBuilder,
          emptyBuilder: emptyBuilder,
          errorBuilder: errorBuilder,
          statePositionBuilder: statePositionBuilder,
          emptyOverlay: emptyOverlay,
          errorOverlay: errorOverlay,
          loadingOverlay: loadingOverlay,
          child: buildGridView<T>(
            items: items,
            itemBuilder: itemBuilder,
            crossAxisCount: crossAxisCount,
            crossAxisSpacing: crossAxisSpacing,
            mainAxisSpacing: mainAxisSpacing,
            childAspectRatio: childAspectRatio,
            onTap: onTap,
            padding: padding,
            controller: controller,
            onLongPress: onLongPress,
          ),
        );
      },
    );
  }
}

class RefreshSliverGrid<T, C extends RefreshPageController>
    extends StatelessWidget {
  final ItemWidgetBuilder<T> itemBuilder;
  final List<T> items;
  final int crossAxisCount;
  final double crossAxisSpacing;
  final double mainAxisSpacing;
  final double childAspectRatio;
  final ItemActionBuilder<T>? onTap;
  final String? tag;
  final bool enablePullUp;
  final bool enablePullDown;
  final ItemActionBuilder<T>? onLongPress;

  /// 自定义加载中状态构建器，优先级高于全局配置
  final LoadingBuilder? loadingBuilder;

  /// 自定义空状态构建器，优先级高于全局配置
  final EmptyBuilder? emptyBuilder;

  /// 自定义错误状态构建器，优先级高于全局配置
  final ErrorBuilder? errorBuilder;

  /// 自定义状态显示位置构建器，允许用户自定义状态显示位置
  final Widget Function(Widget stateWidget)? statePositionBuilder;

  /// 空状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool emptyOverlay;

  /// 错误状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool errorOverlay;

  /// 加载状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool loadingOverlay;

  const RefreshSliverGrid({
    super.key,
    required this.itemBuilder,
    required this.items,
    this.crossAxisCount = 3,
    this.crossAxisSpacing = 10,
    this.mainAxisSpacing = 10,
    this.childAspectRatio = 0.7,
    this.onTap,
    this.tag,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.onLongPress,
    this.loadingBuilder,
    this.emptyBuilder,
    this.errorBuilder,
    this.statePositionBuilder,
    this.emptyOverlay = true,
    this.errorOverlay = true,
    this.loadingOverlay = true,
  });

  @override
  Widget build(BuildContext context) {
    final pageController = Get.find<C>(tag: tag);
    return GetBuilder<C>(
      tag: tag,
      id: pageController.refreshId,
      builder: (_) {
        return RefreshWidget<C>(
          tag: tag,
          enablePullUp: enablePullUp,
          enablePullDown: enablePullDown,
          loadingBuilder: loadingBuilder,
          emptyBuilder: emptyBuilder,
          errorBuilder: errorBuilder,
          statePositionBuilder: statePositionBuilder,
          emptyOverlay: emptyOverlay,
          errorOverlay: errorOverlay,
          loadingOverlay: loadingOverlay,
          child: buildSliverGrid<T>(
            items: items,
            itemBuilder: itemBuilder,
            crossAxisCount: crossAxisCount,
            crossAxisSpacing: crossAxisSpacing,
            mainAxisSpacing: mainAxisSpacing,
            childAspectRatio: childAspectRatio,
            onTap: onTap,
            onLongPress: onLongPress,
          ),
        );
      },
    );
  }
}
